import { defineStore } from "pinia";
import type { ConfigCol, TableData } from "@/utils/struct";
import { ConfigDataType } from "@/utils/enum";
import type { StoreTable } from "@/utils/struct";
const electron = require('electron')
export const useConfigTableStore = defineStore('configTableStore', {
    state: () => {
        return {
            nextId:1,
            tables:[] as StoreTable[],
            tableMap:new Map<number, StoreTable>()
        }
    },
    getters:{
    },
    actions: {
        initData(data:any) {
            console.log("initData",data)
            this.tables.splice(0)
            this.tables.push(...data.tables)
            this.nextId = data.nextId
            this.initTableMap()
        },
        initTableMap() {
            this.tableMap = new Map()
            for (let i = 0; i < this.tables.length; i++) {
                this.tableMap.set(this.tables[i].id, this.tables[i])
            }
        },
        push(table:StoreTable) {
            this.tables.push(table)
            this.tableMap.set(table.id, table)
        },
        // deleteCurPageRow(row:number) {
        //     let rowId = this.curTable.rows[row].id
        //     this.curTable.rows.splice(row, 1)
        //     electron.ipcRenderer.send("deleteTableRow", this.curTable.id, rowId)
        // },
        addTable() {
            let table = {
                exportGroups:[1, 2],
                nextRowId:2,
                id:this.nextId++,
                key:"newTable"+this.nextId,
                name: "新配表",
                desc: "",
                nextHeaderId:2,
                tableHeader: [{id: 1, label:"价值", type:0, key:"value", quoteId:0, exportGroups:[]}],
                rows:[
                    {
                        id: 1,
                        values:[0]
                    }
                ]
            }
            this.push(table)
            return table
        },
        deleteTable(id:number) {
            this.tables = this.tables.filter(item=>item.id != id)
            this.tableMap.delete(id)
        },
        getTable(id:number) {
            // return this.tables.find(item=>item.id==id)
            return this.tableMap.get(id)
        },
        getTableName(id:number, defaultName:string) {
            let tbl = this.tables.find(item=>item.id==id)
            return tbl==null?defaultName : tbl.name
        },
        hasTable(id:number) {
            return this.tables.find(item=>item.id==id) != null
        },
        hasTableRow(id:number, rid:number) {
            let tbl = this.getTable(id);
            if (!tbl) {
                return false
            }
            return tbl.rows.find(item=>item.id==rid) != null
        },
        getTableRow(id:number, eid:number) {
            let tbl = this.getTable(id)
            if (!tbl)
                return null
            let e = tbl.rows.find(item=>item.id==eid)
            return e
        },
        getTableHeader(id:number) {
            let tbl = this.getTable(id)
            if (!tbl)
                return []
            return tbl.tableHeader
        },
    }
})